package com.dlc.beijingrfid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.adapter.AbnormalAdapter;
import com.dlc.beijingrfid.adapter.decoration.RecyclerSpace;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.net.bean.RFIDBean;
import com.dlc.beijingrfid.net.protocol.CountingProto;
import com.lzy.okgo.model.HttpParams;

import butterknife.BindView;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 异常记录
 * Created by YoungeTao on 2017/07/20
 * QQ 2276559259.
 * gmail youngetao@gmail.com
 */
public class AbnormalListActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    private AbnormalAdapter<RFIDBean.DataBean.ListBean> adapter;

    @Override
    protected int getLayoutID() {
        return R.layout.activity_abnormal_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTitleBar.leftExit(this);

        mTitleBar.setTitle(R.string.abnormal_record);

        adapter = new AbnormalAdapter<>();

        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, BaseRecyclerAdapter.CommonHolder holder,
                                    int position) {
                startActivity(InventoryRecordDetailActivity.class);
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerSpace(getResources(), 0, R.dimen.normal_24dp));
        mRecyclerView.setAdapter(adapter);
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        String rfids = intent.getStringExtra("rfids");
        HttpParams params = new HttpParams();
        params.put("rfids",rfids);
        CountingProto.get().getInventory(params).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<RFIDBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogPlus.e(e.getMessage());
                    }

                    @Override
                    public void onNext(RFIDBean rfidBean) {
                        if(rfidBean != null && rfidBean.getData() != null && rfidBean.getData().getList() != null){
                            adapter.setNewData(rfidBean.getData().getList());
                        }
                    }
                });
    }
}
